package com.yiyuwanglu.basecore.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.yiyuwanglu.basecore.common.Pager;
import com.yiyuwanglu.basecore.dao.MongoDBBaseDao;
import com.yiyuwanglu.basecore.service.IWapDataService;
import com.yiyuwanglu.basecore.vo.WapDataVo;

@Service("wapDataService")
public class WapDataServiceImpl implements IWapDataService {

	@Autowired
	private MongoDBBaseDao mongoDBBase;
	
	@Override
	public int shopRecordCount(WapDataVo wap) {
		Criteria ca = Criteria.where("shopId").is(wap.getShopId());

		TypedAggregation<WapDataVo> countAgg = Aggregation.newAggregation(
				WapDataVo.class
				, Aggregation.match(ca)
				, Aggregation.group("wechatId")
					.max("wechatId").as("wechatId")
					.max("createDate").as("createDate")
					.count().as("count")
		);
		
		AggregationResults<WapDataVo> resultsCount = mongoDBBase.aggregate(countAgg, "wap", WapDataVo.class);
		int count = resultsCount.getMappedResults().size();
		
		return count;
	}
	
	@Override
	public List<WapDataVo> shopRecord(WapDataVo wap, Pager page) {
		AggregationResults<WapDataVo> results = null;
		
		Criteria ca = Criteria.where("shopId").is(wap.getShopId());

		TypedAggregation<WapDataVo> agg = Aggregation.newAggregation(
				WapDataVo.class
				, Aggregation.match(ca)
				, Aggregation.group("wechatId")
					.max("wechatId").as("wechatId")
					.max("createDate").as("createDate")
					.count().as("count")
				, Aggregation.sort(Direction.DESC, "createDate")
				, Aggregation.skip((page.getPageSize() * (page.getPageNo() - 1)))
				, Aggregation.limit(page.getPageSize())
		);
		results = mongoDBBase.aggregate(agg, "wap", WapDataVo.class);
		
		return results.getMappedResults();
	}

	@Override
	public Long shopViewCount(WapDataVo wap){
		Query query = new Query();
		
		Criteria ca = new Criteria();
		ca.and("shopId").is(wap.getShopId());
		query.addCriteria(ca);
		
		return mongoDBBase.count("wap", query);
	}
	// new 
	@Override
	public int shopViewCountAll(WapDataVo wap){
		Query query = new Query();
		
		Criteria ca = new Criteria();
		ca.and("shopId").is(wap.getShopId());
		
		
		if(!StringUtils.isBlank(wap.getQueryTimes())){
			ca.and("createDate").regex("^"+wap.getQueryTimes());
		}
		ca.and("type").regex("^11010\\d");
		
		
		query.addCriteria(ca);
		TypedAggregation<WapDataVo> countAgg = Aggregation.newAggregation(
				WapDataVo.class
				, Aggregation.match(ca)
				, Aggregation.group("wechatId")
					.max("wechatId").as("wechatId")
					.max("createDate").as("createDate")
					.count().as("count")
		);
		AggregationResults<WapDataVo> resultsCount = mongoDBBase.aggregate(countAgg, "wap", WapDataVo.class);
		int count = resultsCount.getMappedResults().size();
		return count;
	}
	//店铺访客数
	@Override
	public int shopViewSectionDate(WapDataVo wap){
		Query query = new Query();
		
		Criteria ca = new Criteria();
		ca.and("shopId").is(wap.getShopId());
		if(!StringUtils.isBlank(wap.getMonthStart())){
			ca.and("createDate").gte(wap.getMonthStart());
		}
		ca.and("type").regex("^11010\\d");
//		if(!StringUtils.isBlank(wap.getMonthEnd())){
//			ca.and("createDate").lte(wap.getMonthEnd());
//		}
		query.addCriteria(ca);
		
		TypedAggregation<WapDataVo> countAgg = Aggregation.newAggregation(
				WapDataVo.class
				, Aggregation.match(ca)
				, Aggregation.group("wechatId")
				.max("wechatId").as("wechatId")
				.max("createDate").as("createDate")
				.count().as("count")
		);
		AggregationResults<WapDataVo> resultsCount = mongoDBBase.aggregate(countAgg, "wap", WapDataVo.class);
		int count = resultsCount.getMappedResults().size();
		return count;
	}
	
	//访客数按 数组
	@Override
	public List<WapDataVo>  shopViewSectionList(WapDataVo wap){
		List<WapDataVo> list=null;
		Query query = new Query();
		
		Criteria ca = new Criteria();
		ca.and("shopId").is(wap.getShopId());
		if(!StringUtils.isBlank(wap.getMonthEnd())){
			ca.and("createDate").regex("^"+wap.getMonthEnd());
		}
		if(!StringUtils.isBlank(wap.getMonthStart())){
//			ca.and("createDate").gte(wap.getMonthStart());
		}
		ca.and("type").regex("^11010\\d");
		
		 
		
		
		query.addCriteria(ca);
		
		TypedAggregation<WapDataVo> agg = Aggregation.newAggregation(
				WapDataVo.class
				,Aggregation.match(ca)
//				,Aggregation.project("createDate")    
//				.andExpression("substr(createDate,0,10)").as("publishDate")
//				.andExpression("wechatId").as("wechatId")
				, Aggregation.group("wechatId")
					.max("wechatId").as("wechatId")
//					.max("createDate").as("createDate")
					.count().as("count")
//				, Aggregation.sort(Direction.DESC, "createDate")
					
		);
		
		list = mongoDBBase.aggregate(agg, "wap", WapDataVo.class).getMappedResults();
		
		return list;
	}
	
	
	//店铺浏览量  按时间合计
	@Override
	public Long shopSeeCount(WapDataVo wap){
		
		Query query = new Query();
		
		Criteria ca = new Criteria();
		ca.and("shopId").is(wap.getShopId());
		if(!StringUtils.isBlank(wap.getMonthStart())){
			ca.and("createDate").gte(wap.getMonthStart());
		}
		
		ca.and("type").regex("^11010\\d");
//		if(!StringUtils.isBlank(wap.getMonthEnd())){
//			ca.and("createDate").lte(wap.getMonthEnd());
//		}
		query.addCriteria(ca); 
		
		return mongoDBBase.count("wap", query);
		
	}
	///店铺浏览量 按时间 数组统计
	
	@Override
	public List<WapDataVo> shopSeeDateCount(WapDataVo wap){
		List<WapDataVo> list = null;
		Query query = new Query();
		
		Criteria ca = new Criteria();
		ca.and("shopId").is(wap.getShopId());
		if(!StringUtils.isBlank(wap.getMonthStart())){
			ca.and("createDate").gte(wap.getMonthStart());
		}
		ca.and("type").regex("^11010\\d");
//		if(!StringUtils.isBlank(wap.getMonthEnd())){
//			ca.and("createDate").lte(wap.getMonthEnd());
//		}
		query.addCriteria(ca);
		
		TypedAggregation<WapDataVo> agg = Aggregation.newAggregation(
				WapDataVo.class
				, Aggregation.match(ca)
				,Aggregation.project("createDate")
				.andExpression("substr(createDate,0,10)").as("publishDate")
				, Aggregation.group("publishDate")
					.max("publishDate").as("createDate")
					.count().as("count")
				, Aggregation.sort(Direction.DESC, "createDate")
		);
		list = mongoDBBase.aggregate(agg, "wap", WapDataVo.class).getMappedResults();
		
		return list;
		
	}
	
	@Override
	public Long shopProductViewCount(WapDataVo wap){
		Query query = new Query();
		
		Criteria ca = new Criteria();
		ca.and("shopId").is(wap.getShopId());
		// ca.and("type").is("110200");
		ca.and("type").regex("^11020\\d");
		
		query.addCriteria(ca);
		
		return mongoDBBase.count("wap", query);
	}
	/**
	 *  店铺分享统计   并且自己分享
	 * @param wap
	 * @return
	 */
	@Override
	public Long shopShareCount(WapDataVo wap){
		Query query = new Query();
		
		Criteria ca = new Criteria();
		ca.and("shopId").is(wap.getShopId());
		ca.and("userId").is(wap.getShopId());
		ca.and("type").is("110400");
		
		query.addCriteria(ca);
		
		return mongoDBBase.count("wap", query);
	}
	
	@Override
	public List<WapDataVo> shopProductRecord(WapDataVo wap, Pager page){
		List<WapDataVo> list = null;
		
		Criteria ca = new Criteria();
		if(StringUtils.isNotBlank(wap.getShopId())){
			//多个店铺用，分割
			ca.and("shopId").in(wap.getShopId().split(","));
		}else{
			ca.and("shopId").is(wap.getShopId());
		}
		//新增时间条件
		if(!StringUtils.isBlank(wap.getMonthStart())){
			ca.and("createDate").gte(wap.getMonthStart());
		}
		ca.and("type").regex("^11020\\d");
		if(StringUtils.isNotBlank(wap.getParam())){
			ca.and("param").is(wap.getParam());
		}else{
			ca.and("param").ne("");
		}
		
		TypedAggregation<WapDataVo> agg = Aggregation.newAggregation(
				WapDataVo.class
				, Aggregation.match(ca)
				, Aggregation.group("param")
					.max("param").as("param")
					.max("type").as("type")
					.max("productName").as("productName")
					.max("createDate").as("createDate")
					.count().as("count")
				, Aggregation.sort(Direction.DESC, "count")
				, Aggregation.skip((page.getPageSize() * (page.getPageNo() - 1)))
				, Aggregation.limit(page.getPageSize())
		);
		list = mongoDBBase.aggregate(agg, "wap", WapDataVo.class).getMappedResults();
		
		return list;
	}
	
	@Override
	public List<WapDataVo>  shopProductCountSee(WapDataVo wap,Pager page){
		List<WapDataVo> list = null;
		
		Criteria ca = new Criteria();
		if(StringUtils.isNotBlank(wap.getShopId())){
			//多个店铺用，分割
			ca.and("shopId").in(wap.getShopId().split(","));
		}else{
			ca.and("shopId").is(wap.getShopId());
		}
		ca.and("type").regex("^11020\\d");
		if(StringUtils.isNotBlank(wap.getParam())){
			ca.and("param").is(wap.getParam());
		}else{
			ca.and("param").ne("");
		}
		if(!StringUtils.isBlank(wap.getMonthStart())){
			ca.and("createDate").gte(wap.getMonthStart());
		}
		TypedAggregation<WapDataVo> agg = Aggregation.newAggregation(
				WapDataVo.class
				, Aggregation.match(ca)
				, Aggregation.group("param","wechatId")
						.max("param").as("param")
						.max("wechatId").as("wechatId")
						.max("type").as("type")
						.max("productName").as("productName")
						.max("createDate").as("createDate")
						.count().as("count")
				, Aggregation.group("param")
					.max("param").as("param")
					.max("wechatId").as("wechatId")
					.max("type").as("type")
					.max("productName").as("productName")
					.max("createDate").as("createDate")
						.count().as("count")

				, Aggregation.sort(Direction.DESC, "count")
				, Aggregation.skip((page.getPageSize() * (page.getPageNo() - 1)))
				, Aggregation.limit(page.getPageSize())
		);
		list = mongoDBBase.aggregate(agg, "wap", WapDataVo.class).getMappedResults();
		return list;
	}
	
	@Override
	public int shopProductRecordCount(WapDataVo wap){
		Criteria ca = new Criteria();
		if(StringUtils.isNotBlank(wap.getShopId())){
			//多个店铺用，分割
			ca.and("shopId").in(wap.getShopId().split(","));
		}else{
			ca.and("shopId").is(wap.getShopId());
		}
		//新增时间条件
		if(!StringUtils.isBlank(wap.getMonthStart())){
			ca.and("createDate").gte(wap.getMonthStart());
		}
		ca.and("type").regex("^11020\\d");
		if(StringUtils.isNotBlank(wap.getParam())){
			ca.and("param").is(wap.getParam());
		}else{
			ca.and("param").ne("");
		}

		TypedAggregation<WapDataVo> agg = Aggregation.newAggregation(
				WapDataVo.class
				, Aggregation.match(ca)
				, Aggregation.group("param")
					.max("param").as("param")
					.max("type").as("type")
					.max("productName").as("productName")
					.max("createDate").as("createDate")
		);
		
		return mongoDBBase.aggregate(agg, "wap", WapDataVo.class).getMappedResults().size();
	}
	
	@Override 
	public List<WapDataVo> shopRecordDetail(WapDataVo wap, Pager page){
		List<WapDataVo> list = null;
		
		Criteria ca = new Criteria();
		ca.and("shopId").is(wap.getShopId());
		ca.and("wechatId").is(wap.getWechatId());

		TypedAggregation<WapDataVo> agg = Aggregation.newAggregation(
				WapDataVo.class
				, Aggregation.match(ca)
				, Aggregation.group("param")
					.max("param").as("param")
					.max("type").as("type")
					.max("productName").as("productName")
					.max("createDate").as("createDate")
					.count().as("count")
				, Aggregation.sort(Direction.DESC, "createDate")
				, Aggregation.skip((page.getPageSize() * (page.getPageNo() - 1)))
				, Aggregation.limit(page.getPageSize())
		);
		list = mongoDBBase.aggregate(agg, "wap", WapDataVo.class).getMappedResults();
		
		return list;
	}
	
	@Override
	public int shopRecordDetailCount(WapDataVo wap){
		Criteria ca = new Criteria();
		ca.and("shopId").is(wap.getShopId());
		ca.and("wechatId").is(wap.getWechatId());

		TypedAggregation<WapDataVo> agg = Aggregation.newAggregation(
				WapDataVo.class
				, Aggregation.match(ca)
				, Aggregation.group("param")
					.max("param").as("param")
					.max("type").as("type")
					.max("productName").as("productName")
					.max("createDate").as("createDate")
		);
		
		return mongoDBBase.aggregate(agg, "wap", WapDataVo.class).getMappedResults().size();
	}

	@Override
	public List<WapDataVo> shopProductRecordDetail(WapDataVo wap, Pager page){
		List<WapDataVo> list = null;
		
		Criteria ca = new Criteria();
		if(StringUtils.isNotBlank(wap.getShopId())){
			//多个店铺用，分割
			ca.and("shopId").in(wap.getShopId().split(","));
		}else{
			ca.and("shopId").is(wap.getShopId());
		}	
		//新增时间条件
		if(!StringUtils.isBlank(wap.getMonthStart())){
			ca.and("createDate").gte(wap.getMonthStart());
		}
		ca.and("type").regex("^11020\\d");
		ca.and("param").is(wap.getParam());

		TypedAggregation<WapDataVo> agg = Aggregation.newAggregation(
				WapDataVo.class
				, Aggregation.match(ca)
				, Aggregation.group("param", "wechatId")
					.max("param").as("param")
					.max("wechatId").as("wechatId")
					.max("type").as("type")
					.max("productName").as("productName")
					.max("createDate").as("createDate")
					.count().as("count")
				, Aggregation.sort(Direction.DESC, "createDate")
				, Aggregation.skip((page.getPageSize() * (page.getPageNo() - 1)))
				, Aggregation.limit(page.getPageSize())
		);
		list = mongoDBBase.aggregate(agg, "wap", WapDataVo.class).getMappedResults();
		
		return list;
	}
	
	@Override
	public int shopProductRecordDetailCount(WapDataVo wap){
		Criteria ca = new Criteria();
		if(StringUtils.isNotBlank(wap.getShopId())){
			//多个店铺用，分割
			ca.and("shopId").in(wap.getShopId().split(","));
		}else{
			ca.and("shopId").is(wap.getShopId());
		}
		//新增时间条件
		if(!StringUtils.isBlank(wap.getMonthStart())){
			ca.and("createDate").gte(wap.getMonthStart());
		}
		ca.and("type").regex("^11020\\d");
		ca.and("param").is(wap.getParam());
		
		TypedAggregation<WapDataVo> agg = Aggregation.newAggregation(
				WapDataVo.class
				, Aggregation.match(ca)
				, Aggregation.group("param", "wechatId")
					.max("param").as("param")
					.max("type").as("type")
					.max("productName").as("productName")
					.max("createDate").as("createDate")
		);
		
		return mongoDBBase.aggregate(agg, "wap", WapDataVo.class).getMappedResults().size();
	}
	
	
	@Override 
	public List<WapDataVo> shopUserRecordDetail(WapDataVo wap, Pager page){
		List<WapDataVo> list = null;
		
		Criteria ca = new Criteria();
		ca.and("shopId").is(wap.getShopId());
		ca.and("wechatId").is(wap.getWechatId());

		Query query = new Query();
		query.addCriteria(ca);
		query.with(new Sort(Direction.DESC, "createDate"));
		query.skip((page.getPageSize() * (page.getPageNo() - 1)));
		query.limit(page.getPageSize());
		
		list = mongoDBBase.findByQuery(WapDataVo.class, query, "wap");
		
		return list;
	}
	
	@Override
	public int shopUserRecordDetailCount(WapDataVo wap){
		List<WapDataVo> list = null;
		
		Criteria ca = new Criteria();
		ca.and("shopId").is(wap.getShopId());
		ca.and("wechatId").is(wap.getWechatId());

		Query query = new Query();
		query.addCriteria(ca);
		
		list = mongoDBBase.findByQuery(WapDataVo.class, query, "wap");
		
		return list.size();
	}
	
	
	@Override
	public List<WapDataVo> shopVistSort(WapDataVo wap) {
		List<WapDataVo> list = null;
		Criteria ca = new Criteria();
		if(StringUtils.isNotBlank(wap.getCjmd())){
			//有过滤名单
			ca.and("shopId").in(wap.getCjmd().split(","));
		}else{
			ca.and("shopId").ne(null);//店铺不等于null的
		}
		//时间区间
		if(StringUtils.isNotBlank(wap.getStartVistDate())&&StringUtils.isNotBlank(wap.getEndVistDate())){
			ca.and("createDate").gte(wap.getStartVistDate()).lte(wap.getEndVistDate());
		}else if(StringUtils.isNotBlank(wap.getStartVistDate())){
			ca.and("createDate").gte(wap.getStartVistDate());//时间大于该时间
		}else  if(StringUtils.isNotBlank(wap.getEndVistDate())){
			ca.and("createDate").lte(wap.getEndVistDate());//时间小于结束时间
		}
		
		if(wap.getLimitCount()==null){
			wap.setLimitCount(100);//默认查询100条
		}
		TypedAggregation<WapDataVo> agg = Aggregation.newAggregation(
				WapDataVo.class
				, Aggregation.match(ca)
				, Aggregation.group("shopId")
					.max("shopId").as("shopId")//店铺
					.count().as("count")//累计访问次数
				, Aggregation.sort(Direction.DESC, "count")//根据访问量排名
				, Aggregation.skip(0)
				, Aggregation.limit(wap.getLimitCount())
		);
		list = mongoDBBase.aggregate(agg, "wap", WapDataVo.class).getMappedResults();
		
		return list;
	}
	/***
	 * 查询指定店铺的访问排名
	 */
	@Override
	public WapDataVo shopUserVistSort(WapDataVo wap) {
		List<WapDataVo> list = null;
		Criteria ca = new Criteria();
		ca.and("shopId").is(wap.getShopId());
		//时间区间
		if(StringUtils.isNotBlank(wap.getStartVistDate())&&StringUtils.isNotBlank(wap.getEndVistDate())){
			ca.and("createDate").gte(wap.getStartVistDate()).lte(wap.getEndVistDate());
		}else if(StringUtils.isNotBlank(wap.getStartVistDate())){
			ca.and("createDate").gte(wap.getStartVistDate());//时间大于该时间
		}else  if(StringUtils.isNotBlank(wap.getEndVistDate())){
			ca.and("createDate").lte(wap.getEndVistDate());//时间小于结束时间
		}
		TypedAggregation<WapDataVo> agg = Aggregation.newAggregation(
				WapDataVo.class
				, Aggregation.match(ca)
				, Aggregation.group("shopId")
				.max("shopId").as("shopId")//店铺
				.count().as("count")//累计访问次数
		);
		list = mongoDBBase.aggregate(agg, "wap", WapDataVo.class).getMappedResults();
		WapDataVo wapDataVo = new WapDataVo();
		if(list.size()==0){
			wapDataVo.setCount("0");
		}else{
			wapDataVo = list.get(0);
		}
		Criteria ca2 = new Criteria();
		if(StringUtils.isNotBlank(wap.getCjmd())){
			//有过滤名单
			ca2.and("shopId").in(wap.getCjmd().split(","));
		}else{
			ca2.and("shopId").ne(null);//店铺不等于null的
		}
		//时间区间
		if(StringUtils.isNotBlank(wap.getStartVistDate())&&StringUtils.isNotBlank(wap.getEndVistDate())){
			ca2.and("createDate").gte(wap.getStartVistDate()).lte(wap.getEndVistDate());
		}else if(StringUtils.isNotBlank(wap.getStartVistDate())){
			ca2.and("createDate").gte(wap.getStartVistDate());//时间大于该时间
		}else  if(StringUtils.isNotBlank(wap.getEndVistDate())){
			ca2.and("createDate").lte(wap.getEndVistDate());//时间小于结束时间
		}
		Criteria having = new Criteria();
		having.is(wap.getShopId());
		//该用户的统计数量
		int currentUserCount = Integer.parseInt(wapDataVo.getCount());
		having.and("count").gte(currentUserCount);
		agg = Aggregation.newAggregation(
				WapDataVo.class
				, Aggregation.match(ca2)
				, Aggregation.group("shopId")
				.max("shopId").as("shopId")//店铺
				.count().as("count")//累计访问次数
				, Aggregation.sort(Direction.DESC, "count")//根据访问量排名
				, Aggregation.match(having)
		);
		list = mongoDBBase.aggregate(agg, "wap", WapDataVo.class).getMappedResults();
		wapDataVo.setCurrentSort(list.size()+(currentUserCount==0?1:0));
		if(list.size()>0){
			Integer firstCount = Integer.parseInt(list.get(0).getCount())-currentUserCount;
			wapDataVo.setFirstCount(firstCount);
		}else{
			wapDataVo.setFirstCount(0);
		}
		return wapDataVo;
	}
	/**
	 * 用户访问产品行为统计
	 */
	public List<WapDataVo> userProject(WapDataVo wap,String type) {
		Criteria ca = new Criteria();
		ca.and("type").in(type.split(","));
		ca.and("userMobile").ne(null);
		TypedAggregation<WapDataVo> agg = Aggregation.newAggregation(
				WapDataVo.class
				, Aggregation.match(ca)
				, Aggregation.group("userMobile")
					.max("createDate").as("createDate")
					.last("param").as("param")
					.last("type").as("type")
					.max("userMobile").as("userMobile")
					.last("productName").as("productName")
					, Aggregation.sort(Direction.DESC, "createDate")
					
		);
		
		return mongoDBBase.aggregate(agg, "wap", WapDataVo.class).getMappedResults();
	}
	
	/***
	 * 查询产品  理财师分享次数
	 */
	@Override
	public List<WapDataVo> shopProductShare(WapDataVo wap,Pager page) {
		List<WapDataVo> list = null;
		Query query = new Query();
		TypedAggregation<WapDataVo> agg = productShare(wap, query);
		query.skip((page.getPageSize() * (page.getPageNo() - 1)));
		query.limit(page.getPageSize());
		list = mongoDBBase.aggregate(agg, "wap", WapDataVo.class).getMappedResults();
		return list;
	}

	@Override
	public int shopProductShareCount(WapDataVo wap) {
		Query query = new Query();
		TypedAggregation<WapDataVo> agg = productShare(wap, query);
		return mongoDBBase.aggregate(agg, "wap", WapDataVo.class).getMappedResults().size();
	}

	private TypedAggregation<WapDataVo> productShare(WapDataVo wap, Query query) {
		Criteria ca = new Criteria();
		//产品id
		if(StringUtils.isNotBlank(wap.getParam())){
			ca.and("param").is(wap.getParam());
		}else{
			ca.and("param").regex("^[a-zA-Z0-9-].*?");
		}
		
		if(!StringUtils.isBlank(wap.getMonthStart())){
			ca.and("createDate").gte(wap.getMonthStart());
		}
		//有过滤名单
		ca.and("shopId").in(wap.getShopId().split(","));
		
		//分享产品
		ca.and("type").is("110503");
		query.addCriteria(ca);
		
		TypedAggregation<WapDataVo> agg = null;
		if("ALL".equals(wap.getCjmd())){
			agg = Aggregation.newAggregation(
					WapDataVo.class
					, Aggregation.match(ca)
					, Aggregation.group("param")
					.max("shopId").as("shopId")//店铺
					.count().as("count")
					.max("param").as("param")
					.max("createDate").as("createDate")
					, Aggregation.sort(Direction.DESC, "createDate")
					);
		}else{
			agg = Aggregation.newAggregation(
					WapDataVo.class
					, Aggregation.match(ca)
					, Aggregation.group("shopId")
					.max("shopId").as("shopId")//店铺
					.count().as("count")
					.max("param").as("param")
					);
		}
		return agg;
	}

	@Override
	public List<WapDataVo> shareShopProductSeeDetail(WapDataVo wap,Pager page) {
		List<WapDataVo> list = null;
		Query query = new Query();
		Criteria ca = new Criteria();
		//产品id
		ca.and("param").is(wap.getParam());
		if(!StringUtils.isBlank(wap.getMonthStart())){
			ca.and("createDate").gte(wap.getMonthStart());
		}
		//有过滤名单
		ca.and("shopId").in(wap.getShopId().split(","));
		//分享产品
		ca.and("type").is("110503");
		query.addCriteria(ca);
		TypedAggregation<WapDataVo> agg = Aggregation.newAggregation(
				WapDataVo.class
				, Aggregation.match(ca)
		);
		query.skip((page.getPageSize() * (page.getPageNo() - 1)));
		query.limit(page.getPageSize());
		list = mongoDBBase.aggregate(agg, "wap", WapDataVo.class).getMappedResults();
		return list;
	}

	//用户分享某个产品    被查看次数  
	@Override
	public int shopProductSeeDetailCount(WapDataVo wap){
		Criteria ca = new Criteria();
		if(StringUtils.isNotBlank(wap.getShopId())){
			//多个店铺用，分割
			ca.and("shopId").in(wap.getShopId().split(","));
		}else{
			ca.and("shopId").is(wap.getShopId());
		}
		//新增时间条件
		if(!StringUtils.isBlank(wap.getMonthStart())){
			ca.and("createDate").gte(wap.getMonthStart());
		}
		ca.and("type").regex("^11020\\d");
		ca.and("param").is(wap.getParam());
		
		TypedAggregation<WapDataVo> agg = Aggregation.newAggregation(
				WapDataVo.class
				, Aggregation.match(ca)
				, Aggregation.group("param", "wechatId")
					.max("param").as("param")
					.max("type").as("type")
					.max("productName").as("productName")
					.max("createDate").as("createDate")
			, Aggregation.sort(Direction.DESC, "createDate")
		);
		
		return mongoDBBase.aggregate(agg, "wap", WapDataVo.class).getMappedResults().size();
	}
	//用户分享某个产品    被查看次数  
	@Override 
	public List<WapDataVo> shopProductSeeDetail(WapDataVo wap, Pager page){
		List<WapDataVo> list = null;
		Query query = new Query();
		Criteria ca = new Criteria();
		if(StringUtils.isNotBlank(wap.getShopId())){
			//多个店铺用，分割
			ca.and("shopId").in(wap.getShopId().split(","));
		}else{
			ca.and("shopId").is(wap.getShopId());
		}
		//新增时间条件
		if(!StringUtils.isBlank(wap.getMonthStart())){
			ca.and("createDate").gte(wap.getMonthStart());
		}
		ca.and("type").regex("^11020\\d");
		ca.and("param").is(wap.getParam());
		
		TypedAggregation<WapDataVo> agg = Aggregation.newAggregation(
				WapDataVo.class
				, Aggregation.match(ca)
				, Aggregation.group("param", "wechatId")
					.max("param").as("param")
					.max("type").as("type")
					.max("productName").as("productName")
					.max("createDate").as("createDate")
			, Aggregation.sort(Direction.DESC, "createDate")
		);
		query.skip((page.getPageSize() * (page.getPageNo() - 1)));
		query.limit(page.getPageSize());
		list = mongoDBBase.aggregate(agg, "wap", WapDataVo.class).getMappedResults();
		return list;
	}
}
